package com.youboy.wechatapi.utils.kwfilter;

import java.util.HashMap;
import java.util.List;

import com.youboy.wechatapi.utils.db.MyDBCon;

public class KeyWord {
	public static final String TABLE_MGC = "mgc";// 敏感词
	public static final String TABLE_WFC = "wfc";// 违法词
	public static final String TABLE_ZLC = "zlc";// 专利词
	public static final String TABLE_FQC = "fqc";// 分歧词
	public static final String TABLE_JDC = "jdc";// 绝对词
	public static final String TABLE_DGC = "dgc";// 过滤字（包含一些特殊词）
	public static final String TABLE_COM_SINGLEWORD = "com_singleword";// 公司名称过滤（一级）
	public static final String TABLE_COM_TWOMOREWORD = "com_twomoreword";// 公司名称过滤（二级）
	private HashMap<String, String> mgc_map = null;
	private HashMap<String, String> wfc_map = null;
	private HashMap<String, String> zlc_map = null;
	private HashMap<String, String> fqc_map = null;
	private HashMap<String, String> jdc_map = null;
	private HashMap<String, String> dgc_map = null;
	private HashMap<String, String> com_singleword_map = null;
	private HashMap<String, String> com_twomoreword_map = null;
	private HashMap<String, Integer> firstword_map = new HashMap<String, Integer>();
	private HashMap<String, Integer> firstword_fqc_map = new HashMap<String, Integer>();
	private static KeyWord singleton;

	private KeyWord() {

	}

	public synchronized static KeyWord getInstance() {
		if (singleton == null) {
			singleton = new KeyWord();
		}
		return singleton;
	}

	public synchronized HashMap<String, String> getMgcMap() {
		if (mgc_map == null) {
			mgc_map = getWordMapDb(TABLE_MGC, "");
		}
		return mgc_map;
	}

	public synchronized HashMap<String, String> getWfcMap() {
		if (wfc_map == null) {
			wfc_map = getWordMapDb(TABLE_WFC, "");
		}
		return wfc_map;
	}

	public synchronized HashMap<String, String> getZlcMap() {
		if (zlc_map == null) {
			zlc_map = getWordMapDb(TABLE_ZLC, "");
		}
		return zlc_map;
	}

	public synchronized HashMap<String, String> getFqcMap() {
		if (fqc_map == null) {
			fqc_map = getWordMapDb(TABLE_FQC, "");
		}
		return fqc_map;
	}

	public synchronized HashMap<String, String> getJdcMap() {
		if (jdc_map == null) {
			jdc_map = getWordMapDb(TABLE_JDC, "");
		}
		return jdc_map;
	}

	public synchronized HashMap<String, String> getDgcMap() {
		if (dgc_map == null) {
			dgc_map = getWordMapDb(TABLE_DGC, "");
		}
		return dgc_map;
	}

	public synchronized HashMap<String, String> getCom1Map() {
		if (com_singleword_map == null) {
			com_singleword_map = getWordMapDb(TABLE_COM_SINGLEWORD, "");
		}
		return com_singleword_map;
	}

	public synchronized HashMap<String, String> getCom2Map() {
		if (com_twomoreword_map == null) {
			com_twomoreword_map = getWordMapDb(TABLE_COM_TWOMOREWORD, "");
		}
		return com_twomoreword_map;
	}

	public synchronized HashMap<String, Integer> getFirstwordMap() {
		if (firstword_map == null) {
			firstword_map = new HashMap<String, Integer>();
		}
		return firstword_map;
	}

	public synchronized HashMap<String, Integer> getFirstwordFqcMap() {
		if (firstword_fqc_map == null) {
			firstword_fqc_map = new HashMap<String, Integer>();
		}
		return firstword_fqc_map;
	}

	public boolean reload() {
		try {
			mgc_map = null;
			wfc_map = null;
			zlc_map = null;
			fqc_map = null;
			jdc_map = null;
			com_singleword_map = null;
			com_twomoreword_map = null;
			KeyWord.getInstance().getMgcMap();
			KeyWord.getInstance().getWfcMap();
			KeyWord.getInstance().getZlcMap();
			KeyWord.getInstance().getFqcMap();
			KeyWord.getInstance().getJdcMap();
			KeyWord.getInstance().getCom1Map();
			KeyWord.getInstance().getCom2Map();
			KeyWord.getInstance().getDgcMap();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean restart() {
		try {
			singleton = null;
			getInstance();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	private HashMap<String, String> getWordMapDb(String tablename,
			String replace) {
		HashMap<String, String> wordMap = new HashMap<String, String>();
		String theWord = null;
		HashMap<String, Integer> firstword = getFirstwordMap();
		HashMap<String, Integer> firstword_fqc = getFirstwordFqcMap();
		String ss = null;
		if (tablename.equals(TABLE_FQC)) {
			MyDBCon myCon = new MyDBCon("kwfilter_db");
			List<List<String>> list = myCon.getListResultSet("select kw from "
					+ tablename, null, null);
			for (int i = 0; i < list.size(); i++) {
				theWord = list.get(i).get(0);
				if (theWord != null && !"".equals(theWord.trim())) {
					String[] sss = theWord.trim().toLowerCase().split("@@");
					if (sss[0] != null && !sss[0].equals("")) {
						if (firstword_fqc.containsKey(sss[0].toCharArray()[0]
								+ "")
								&& firstword_fqc.get(sss[0].toCharArray()[0]
										+ "") > sss[0].length()) {
							// do nothing
						} else {
							firstword_fqc.put(sss[0].toCharArray()[0] + "",
									sss[0].length());
						}
						wordMap.put(sss[0], sss.length == 1 ? "" : sss[1]);
					}
				}
			}
		} else if (tablename.equals(TABLE_COM_SINGLEWORD)) {
			MyDBCon myCon = new MyDBCon("kwfilter_db");
			List<List<String>> list = myCon.getListResultSet("select kw from "
					+ tablename, null, null);
			for (int i = 0; i < list.size(); i++) {
				theWord = list.get(i).get(0);
				if (theWord != null && !"".equals(theWord.trim())) {
					wordMap.put(theWord.trim().toLowerCase(), replace);
				}
			}
		} else if (tablename.equals(TABLE_ZLC)) {
			MyDBCon myCon = new MyDBCon("kwfilter_db");
			List<List<String>> list = myCon.getListResultSet("select kw from "
					+ tablename, null, null);
			for (int i = 0; i < list.size(); i++) {
				theWord = list.get(i).get(0);
				if (theWord != null && !"".equals(theWord.trim())) {
					String[] sss = theWord.trim().toLowerCase().split("@@");
					if (sss[0] != null && !sss[0].equals("")) {
						if (firstword.containsKey(sss[0].toCharArray()[0] + "")
								&& firstword.get(sss[0].toCharArray()[0] + "") > sss[0]
										.length()) {
							// do nothing
						} else {
							firstword.put(sss[0].toCharArray()[0] + "",
									sss[0].length());
						}
						wordMap.put(sss[0], sss.length == 1 ? "" : sss[1]);
					}
				}
			}
		} else {
			MyDBCon myCon = new MyDBCon("kwfilter_db");
			List<List<String>> list = myCon.getListResultSet("select kw from "
					+ tablename, null, null);
			for (int i = 0; i < list.size(); i++) {
				theWord = list.get(i).get(0);
				if (theWord != null && !"".equals(theWord.trim())) {
					ss = theWord.trim().toLowerCase();
					if (firstword.containsKey(ss.toCharArray()[0] + "")
							&& firstword.get(ss.toCharArray()[0] + "") > ss
									.length()) {
						
					} else {
						firstword.put(theWord.trim().toLowerCase()
								.toCharArray()[0]
								+ "", ss.length());
					}
					wordMap.put(theWord.trim().toLowerCase(), replace);
				}
			}
		}
		return wordMap;
	}

	public String filter_in(String content, String sta, String usid) {
		if (content == null || content.length() <= 0) {
			return "0";
		}
		if (usid == null) {
			usid = "0";
		}
		HashMap<String, String> mgc_map = KeyWord.getInstance().getMgcMap();//www.youboy.com有部分没有过滤
		HashMap<String, String> wfc_map = KeyWord.getInstance().getWfcMap();//过滤
		HashMap<String, String> zlc_map = KeyWord.getInstance().getZlcMap();//过滤
		HashMap<String, String> fqc_map = KeyWord.getInstance().getFqcMap();//不过滤
		HashMap<String, String> jdc_map = KeyWord.getInstance().getJdcMap();//过滤
		HashMap<String, String> dgc_map = KeyWord.getInstance().getDgcMap();//过滤词
		HashMap<String, Integer> firstword_map = KeyWord.getInstance()
				.getFirstwordMap();
		HashMap<String, Integer> firstword_fqc_map = KeyWord.getInstance()
				.getFirstwordFqcMap();
		HashMap<String, String> com_singleword_map = KeyWord.getInstance()
				.getCom1Map();
		HashMap<String, String> com_twomoreword_map = KeyWord.getInstance()
				.getCom2Map();

		int result_type = 0;
		int dgc_type = 0;
		char[] ch = content.toCharArray();
		StringBuffer ret = new StringBuffer();
		StringBuffer word = new StringBuffer();
		int start = 0;
		int step = 0;
		int max_length = 1;
		for (int i = 0; i < ch.length; i++) {
			if (step == 0) {
				start = i;
				step = 1;
			}
			char c = ch[i];
			word.append(c);
			String tmp = word.toString();
			if (i == start) {
				if (!firstword_fqc_map.containsKey(tmp.toLowerCase())) {
					step = 0;
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
					continue;
				} else {
					max_length = firstword_fqc_map.get(tmp.toLowerCase());
				}
			}
			if (fqc_map.containsKey(tmp.toLowerCase())) {
				ret.append(fqc_map.get(tmp.toLowerCase()));
				step = 0;
			} else if (word.length() >= max_length || i >= ch.length - 1) {
				step = 0;
				ret.append(ch[start]);
				word.delete(0, word.length());
				i = start;
			}
		}
		ch = ret.toString().toCharArray();
		word = new StringBuffer();
		start = 0;
		step = 0;
		if (sta.equals("0")) {
			for (int i = 0; i < ch.length; i++) {
				if (step == 0) {
					start = i;
					step = 1;
				}
				char c = ch[i];
				word.append(c);
				String tmp = word.toString();
				if (i == start) {
					if (com_singleword_map.containsKey(tmp.toLowerCase())) {
						result_type = 1;
						break;
					}
					if (!firstword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						word.delete(0, word.length());
						i = start;
						continue;
					} else {
						max_length = firstword_map.get(tmp.toLowerCase());
					}
				}
				if (jdc_map.containsKey(tmp.toLowerCase())) {
					result_type = 3;
					break;
				} else if (zlc_map.containsKey(tmp.toLowerCase())) {
					if (!zlc_map.get(tmp.toLowerCase()).equals(usid)) {
						result_type = 2;
						break;
					} else {
						step = 0;
						word.delete(0, word.length());
						i = start;
					}
				} else if (wfc_map.containsKey(tmp.toLowerCase())) {
					result_type = 2;
					break;
				} else if (com_twomoreword_map.containsKey(tmp.toLowerCase())) {
					result_type = 2;
					break;
				} else if (mgc_map.containsKey(tmp.toLowerCase())) {
					result_type = 1;
					break;
				} else if (word.length() > 1
						&& dgc_map.containsKey(tmp.toLowerCase())) {
					dgc_type = 4;
				} else if (word.length() >= max_length || i >= ch.length - 1) {
					step = 0;
					if (dgc_map.containsKey(String.valueOf(ch[start]))) {
						dgc_type = 4;
					}
					word.delete(0, word.length());
					i = start;
				}
			}
		} else if (sta.equals("1")) {
			for (int i = 0; i < ch.length; i++) {
				if (step == 0) {
					start = i;
					step = 1;
				}
				char c = ch[i];
				word.append(c);
				String tmp = word.toString();
				if (i == start) {
					if (!firstword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						word.delete(0, word.length());
						i = start;
						continue;
					} else {
						max_length = firstword_map.get(tmp.toLowerCase());
					}
				}
				if (jdc_map.containsKey(tmp.toLowerCase())) {
					result_type = 3;
					break;
				} else if (zlc_map.containsKey(tmp.toLowerCase())) {
					if (!zlc_map.get(tmp.toLowerCase()).equals(usid)) {
						result_type = 2;
						break;
					} else {
						step = 0;
						word.delete(0, word.length());
						i = start;
					}
				} else if (wfc_map.containsKey(tmp.toLowerCase())) {
					result_type = 2;
					break;
				} else if (mgc_map.containsKey(tmp.toLowerCase())) {
					result_type = 1;
					break;
				} else if (word.length() > 1
						&& dgc_map.containsKey(tmp.toLowerCase())) {
					dgc_type = 4;
				} else if (word.length() >= max_length || i >= ch.length - 1) {
					step = 0;
					if (dgc_map.containsKey(String.valueOf(ch[start]))) {
						dgc_type = 4;
					}
					word.delete(0, word.length());
					i = start;
				}
			}
		}

		if (result_type == 0 && dgc_type == 4) {
			result_type = dgc_type;
		}

		return Integer.toString(result_type);

	}

	public String filter_out(String content, String sta, String usid) {
		if (content == null || content.length() <= 0) {
			return "";
		}
		if (usid == null) {
			usid = "0";
		}
		HashMap<String, String> mgc_map = KeyWord.getInstance().getMgcMap();
		HashMap<String, String> wfc_map = KeyWord.getInstance().getWfcMap();
		HashMap<String, String> zlc_map = KeyWord.getInstance().getZlcMap();
		HashMap<String, String> fqc_map = KeyWord.getInstance().getFqcMap();
		HashMap<String, String> jdc_map = KeyWord.getInstance().getJdcMap();
		HashMap<String, Integer> firstword_map = KeyWord.getInstance()
				.getFirstwordMap();
		HashMap<String, Integer> firstword_fqc_map = KeyWord.getInstance()
				.getFirstwordFqcMap();
		HashMap<String, String> com_singleword_map = KeyWord.getInstance()
				.getCom1Map();
		HashMap<String, String> com_twomoreword_map = KeyWord.getInstance()
				.getCom2Map();

		char[] ch = content.toCharArray();
		StringBuffer ret = new StringBuffer();
		StringBuffer word = new StringBuffer();
		int start = 0;
		int step = 0;
		int max_length = 1;
		for (int i = 0; i < ch.length; i++) {
			if (step == 0) {
				start = i;
				step = 1;
			}
			char c = ch[i];
			word.append(c);
			String tmp = word.toString();
			if (i == start) {
				if (!firstword_fqc_map.containsKey(tmp.toLowerCase())) {
					step = 0;
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
					continue;
				} else {
					max_length = firstword_fqc_map.get(tmp.toLowerCase());
				}
			}
			if (fqc_map.containsKey(tmp.toLowerCase())) {
				ret.append(fqc_map.get(tmp.toLowerCase()));
				step = 0;
			} else if (word.length() >= max_length || i >= ch.length - 1) {
				step = 0;
				ret.append(ch[start]);
				word.delete(0, word.length());
				i = start;
			}
		}

		ch = ret.toString().toCharArray();
		ret = new StringBuffer();
		word = new StringBuffer();
		start = 0;
		step = 0;

		if (sta.equals("0")) {
			for (int i = 0; i < ch.length; i++) {
				if (step == 0) {
					start = i;
					step = 1;
				}
				char c = ch[i];
				word.append(c);
				String tmp = word.toString();
				if (i == start) {
					if (com_singleword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						ret.append("");
						word.delete(0, word.length());
						i = start;
						continue;
					}
					if (!firstword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						ret.append(ch[start]);
						word.delete(0, word.length());
						i = start;
						continue;
					} else {
						max_length = firstword_map.get(tmp.toLowerCase());
					}
				}
				if (jdc_map.containsKey(tmp.toLowerCase())) {
					ret.append("");
					step = 0;
				} else if (zlc_map.containsKey(tmp.toLowerCase())) {
					if (!zlc_map.get(tmp.toLowerCase()).equals(usid)) {
						ret.append("");
						step = 0;
					} else {
						step = 0;
						ret.append(ch[start]);
						word.delete(0, word.length());
						i = start;
					}
				} else if (wfc_map.containsKey(tmp.toLowerCase())) {
					ret.append("");
					step = 0;
				} else if (mgc_map.containsKey(tmp.toLowerCase())) {
					ret.append("");
					step = 0;
				} else if (com_twomoreword_map.containsKey(tmp.toLowerCase())) {
					ret.append("");
					step = 0;
				} else if (word.length() >= max_length || i >= ch.length - 1) {
					step = 0;
					
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
				}

			}
		} else if (sta.equals("1")) {
			for (int i = 0; i < ch.length; i++) {
				if (step == 0) {
					start = i;
					step = 1;
				}
				char c = ch[i];
				word.append(c);
				String tmp = word.toString();
				if (i == start) {
					if (!firstword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						ret.append(ch[start]);
						word.delete(0, word.length());
						i = start;
						continue;
					} else {
						max_length = firstword_map.get(tmp.toLowerCase());
					}
				}
				if (jdc_map.containsKey(tmp.toLowerCase())) {
					ret.append("");
					step = 0;
				} else if (zlc_map.containsKey(tmp.toLowerCase())) {
					if (!zlc_map.get(tmp.toLowerCase()).equals(usid)) {
						ret.append("");
						step = 0;
					} else {
						step = 0;
						ret.append(ch[start]);
						word.delete(0, word.length());
						i = start;
					}
				} else if (wfc_map.containsKey(tmp.toLowerCase())) {
					ret.append("");
					step = 0;
				} else if (mgc_map.containsKey(tmp.toLowerCase())) {
					ret.append("");
					step = 0;
				} else if (word.length() >= max_length || i >= ch.length - 1) {
					step = 0;
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
				}
			}
		}

		String rs = ret.toString().replace("⊙", "");
		return rs;
	}

	public String filter_str(String content, String sta, String usid) {
		if (content == null || content.length() <= 0) {
			return "0";
		}
		if (usid == null) {
			usid = "0";
		}
		HashMap<String, String> mgc_map = KeyWord.getInstance().getMgcMap();
		HashMap<String, String> wfc_map = KeyWord.getInstance().getWfcMap();
		HashMap<String, String> zlc_map = KeyWord.getInstance().getZlcMap();
		HashMap<String, String> fqc_map = KeyWord.getInstance().getFqcMap();
		HashMap<String, String> jdc_map = KeyWord.getInstance().getJdcMap();
		HashMap<String, Integer> firstword_map = KeyWord.getInstance()
				.getFirstwordMap();
		HashMap<String, Integer> firstword_fqc_map = KeyWord.getInstance()
				.getFirstwordFqcMap();
		@SuppressWarnings("unused")
		HashMap<String, String> com_singleword_map = KeyWord.getInstance()
				.getCom1Map();
		HashMap<String, String> com_twomoreword_map = KeyWord.getInstance()
				.getCom2Map();

		char[] ch = content.toCharArray();
		StringBuffer ret = new StringBuffer();
		StringBuffer word = new StringBuffer();
		String str = "";
		int start = 0;
		int step = 0;
		int max_length = 1;
		for (int i = 0; i < ch.length; i++) {
			if (step == 0) {
				start = i;
				step = 1;
			}
			char c = ch[i];
			word.append(c);
			String tmp = word.toString();
			if (i == start) {
				if (!firstword_fqc_map.containsKey(tmp.toLowerCase())) {
					step = 0;
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
					continue;
				} else {
					max_length = firstword_fqc_map.get(tmp.toLowerCase());
				}
			}
			if (fqc_map.containsKey(tmp.toLowerCase())) {
				ret.append(fqc_map.get(tmp.toLowerCase()));
				step = 0;
			} else if (word.length() >= max_length || i >= ch.length - 1) {
				step = 0;
				ret.append(ch[start]);
				word.delete(0, word.length());
				i = start;
			}
		}
		ch = ret.toString().toCharArray();
		word = new StringBuffer();
		start = 0;
		step = 0;

		for (int i = 0; i < ch.length; i++) {
			if (step == 0) {
				start = i;
				step = 1;
			}
			char c = ch[i];
			word.append(c);
			String tmp = word.toString();
			if (i == start) {
				if (!firstword_map.containsKey(tmp.toLowerCase())) {
					step = 0;
					word.delete(0, word.length());
					i = start;
					continue;
				} else {
					max_length = firstword_map.get(tmp.toLowerCase());
				}
			}
			// if (com_singleword_map.containsKey(tmp.toLowerCase())) {
			// break;
			// } else
			if (com_twomoreword_map.containsKey(tmp.toLowerCase())) {
				str = tmp;
				break;
			} else if (jdc_map.containsKey(tmp.toLowerCase())) {
				str = tmp;
				break;
			} else if (zlc_map.containsKey(tmp.toLowerCase())) {
				if (!zlc_map.get(tmp.toLowerCase()).equals(usid)) {
					str = tmp;
					break;
				} else {
					step = 0;
					word.delete(0, word.length());
					i = start;
				}
			} else if (wfc_map.containsKey(tmp.toLowerCase())) {
				str = tmp;
				break;
			} else if (mgc_map.containsKey(tmp.toLowerCase())) {
				str = tmp;
				break;
			} else if (word.length() >= max_length || i >= ch.length - 1) {
				step = 0;
				word.delete(0, word.length());
				i = start;
			}
		}
		return str;
	}

	public String filter_search(String content) {
		if (content == null || content.length() <= 0) {
			return "";
		}
		HashMap<String, String> fqc_map = KeyWord.getInstance().getFqcMap();
		HashMap<String, String> jdc_map = KeyWord.getInstance().getJdcMap();
		HashMap<String, String> wfc_map = KeyWord.getInstance().getWfcMap();
		HashMap<String, Integer> firstword_map = KeyWord.getInstance()
				.getFirstwordMap();
		HashMap<String, Integer> firstword_fqc_map = KeyWord.getInstance()
				.getFirstwordFqcMap();

		char[] ch = content.toCharArray();
		StringBuffer ret = new StringBuffer();
		StringBuffer word = new StringBuffer();
		int start = 0;
		int step = 0;
		int max_length = 1;
		for (int i = 0; i < ch.length; i++) {
			if (step == 0) {
				start = i;
				step = 1;
			}
			char c = ch[i];
			word.append(c);
			String tmp = word.toString();
			if (i == start) {
				if (!firstword_fqc_map.containsKey(tmp.toLowerCase())) {
					step = 0;
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
					continue;
				} else {
					max_length = firstword_fqc_map.get(tmp.toLowerCase());
				}
			}
			if (fqc_map.containsKey(tmp.toLowerCase())) {
				ret.append(fqc_map.get(tmp.toLowerCase()));
				step = 0;
			} else if (word.length() >= max_length || i >= ch.length - 1) {
				step = 0;
				ret.append(ch[start]);
				word.delete(0, word.length());
				i = start;
			}
		}
		ch = ret.toString().toCharArray();
		ret = new StringBuffer();
		word = new StringBuffer();
		start = 0;
		step = 0;

		for (int i = 0; i < ch.length; i++) {
			if (step == 0) {
				start = i;
				step = 1;
			}
			char c = ch[i];
			word.append(c);
			String tmp = word.toString();
			if (i == start) {
				if (!firstword_map.containsKey(tmp.toLowerCase())) {
					step = 0;
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
					continue;
				} else {
					max_length = firstword_map.get(tmp.toLowerCase());
				}
			}
			if (jdc_map.containsKey(tmp.toLowerCase())) {
				ret.append("");
				step = 0;
			} else if (wfc_map.containsKey(tmp.toLowerCase())) {
				ret.append("");
				step = 0;
			} else if (word.length() >= max_length || i >= ch.length - 1) {
				step = 0;
				ret.append(ch[start]);
				word.delete(0, word.length());
				i = start;
			}
		}

		String rs = ret.toString().replace("⊙", "");
		return rs;
	}

	public String filter_jk(String content, String sta, String usid,
			String mgcstyle1, String mgcstyle2, String wfcstyle1,
			String wfcstyle2, String dgcstyle1, String dgcstyle2) {
		if (content == null || content.length() <= 0) {
			return "";
		}
		if (usid == null) {
			usid = "0";
		}
		HashMap<String, String> mgc_map = KeyWord.getInstance().getMgcMap();
		HashMap<String, String> wfc_map = KeyWord.getInstance().getWfcMap();
		HashMap<String, String> zlc_map = KeyWord.getInstance().getZlcMap();
		HashMap<String, String> fqc_map = KeyWord.getInstance().getFqcMap();
		HashMap<String, String> jdc_map = KeyWord.getInstance().getFqcMap();
		HashMap<String, String> dgc_map = KeyWord.getInstance().getDgcMap();
		HashMap<String, Integer> firstword_map = KeyWord.getInstance()
				.getFirstwordMap();
		HashMap<String, Integer> firstword_fqc_map = KeyWord.getInstance()
				.getFirstwordFqcMap();
		HashMap<String, String> com_twomoreword_map = KeyWord.getInstance()
				.getCom2Map();

		char[] ch = content.toCharArray();
		StringBuffer ret = new StringBuffer();
		StringBuffer word = new StringBuffer();
		int start = 0;
		int step = 0;
		int max_length = 1;
		for (int i = 0; i < ch.length; i++) {
			if (step == 0) {
				start = i;
				step = 1;
			}
			char c = ch[i];
			word.append(c);
			String tmp = word.toString();
			if (i == start) {
				if (!firstword_fqc_map.containsKey(tmp.toLowerCase())) {
					step = 0;
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
					continue;
				} else {
					max_length = firstword_fqc_map.get(tmp.toLowerCase());
				}
			}
			if (fqc_map.containsKey(tmp.toLowerCase())) {
				ret.append(fqc_map.get(tmp.toLowerCase()));
				step = 0;
			} else if (word.length() >= max_length || i >= ch.length - 1) {
				step = 0;
				ret.append(ch[start]);
				word.delete(0, word.length());
				i = start;
			}
		}
		ch = ret.toString().toCharArray();
		ret = new StringBuffer();
		word = new StringBuffer();
		start = 0;
		step = 0;

		if (sta.equals("0")) {
			for (int i = 0; i < ch.length; i++) {
				if (step == 0) {
					start = i;
					step = 1;
				}
				char c = ch[i];

				word.append(c);
				String tmp = word.toString();
				if (i == start) {
					if (!firstword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						ret.append(ch[start]);
						word.delete(0, word.length());
						i = start;
						continue;
					} else {
						max_length = firstword_map.get(tmp.toLowerCase());
					}
				}
				if (jdc_map.containsKey(tmp.toLowerCase())) {
					ret.append(wfcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(wfcstyle2);
					step = 0;
				} else if (zlc_map.containsKey(tmp.toLowerCase())) {
					if (!zlc_map.get(tmp.toLowerCase()).equals(usid)) {
						ret.append(wfcstyle1);
						ret.append(tmp.toLowerCase());
						ret.append(wfcstyle2);
						step = 0;
					} else {
						step = 0;
						ret.append(ch[start]);
						word.delete(0, word.length());
						i = start;
					}
				} else if (wfc_map.containsKey(tmp.toLowerCase())) {
					ret.append(wfcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(wfcstyle2);
					step = 0;
				} else if (mgc_map.containsKey(tmp.toLowerCase())) {
					ret.append(mgcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(mgcstyle2);
					step = 0;
				} else if (com_twomoreword_map.containsKey(tmp.toLowerCase())) {
					ret.append(wfcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(wfcstyle2);
					step = 0;
				} else if (word.length() > 1
						&& dgc_map.containsKey(tmp.toLowerCase())) {
					ret.append(dgcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(dgcstyle2);
					step = 0;
				} else if (word.length() >= max_length || i >= ch.length - 1) {
					step = 0;
					if (dgc_map.containsKey(String.valueOf(ch[start]))) {
						ret.append(dgcstyle1);
						ret.append(ch[start]);
						ret.append(dgcstyle2);
					} else {
						ret.append(ch[start]);
					}
					word.delete(0, word.length());
					i = start;
				}
			}
		} else if (sta.equals("1")) {
			for (int i = 0; i < ch.length; i++) {
				if (step == 0) {
					start = i;
					step = 1;
				}
				char c = ch[i];
				word.append(c);
				String tmp = word.toString();
				if (i == start) {
					if (!firstword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						ret.append(ch[start]);
						word.delete(0, word.length());
						i = start;
						continue;
					} else {
						max_length = firstword_map.get(tmp.toLowerCase());
					}
				}
				if (jdc_map.containsKey(tmp.toLowerCase())) {
					ret.append(wfcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(wfcstyle2);
					step = 0;
				} else if (zlc_map.containsKey(tmp.toLowerCase())) {
					if (!zlc_map.get(tmp.toLowerCase()).equals(usid)) {
						ret.append(wfcstyle1);
						ret.append(tmp.toLowerCase());
						ret.append(wfcstyle2);
						step = 0;
					} else {
						step = 0;
						ret.append(ch[start]);
						word.delete(0, word.length());
						i = start;
					}
				} else if (wfc_map.containsKey(tmp.toLowerCase())) {
					ret.append(wfcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(wfcstyle2);
					step = 0;
				} else if (mgc_map.containsKey(tmp.toLowerCase())) {
					ret.append(mgcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(mgcstyle2);
					step = 0;
				} else if (com_twomoreword_map.containsKey(tmp.toLowerCase())) {
					ret.append(wfcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(wfcstyle2);
					step = 0;
				} else if (word.length() > 1
						&& dgc_map.containsKey(tmp.toLowerCase())) {
					ret.append(dgcstyle1);
					ret.append(tmp.toLowerCase());
					ret.append(dgcstyle2);
					step = 0;
				} else if (word.length() >= max_length || i >= ch.length - 1) {
					step = 0;
					if (dgc_map.containsKey(String.valueOf(ch[start]))) {
						ret.append(dgcstyle1);
						ret.append(ch[start]);
						ret.append(dgcstyle2);
					} else {
						ret.append(ch[start]);
					}
					word.delete(0, word.length());
					i = start;
				}
			}
		}

		String rs = ret.toString().replace("⊙", "");
		return rs;
	}
	
	public String filter_in_noZlc(String content, String sta, String usid) {
		if (content == null || content.length() <= 0) {
			return "0";
		}
		if (usid == null) {
			usid = "0";
		}
		HashMap<String, String> mgc_map = KeyWord.getInstance().getMgcMap();
		HashMap<String, String> wfc_map = KeyWord.getInstance().getWfcMap();
		HashMap<String, String> fqc_map = KeyWord.getInstance().getFqcMap();
		HashMap<String, String> jdc_map = KeyWord.getInstance().getJdcMap();
		HashMap<String, String> dgc_map = KeyWord.getInstance().getDgcMap();
		HashMap<String, Integer> firstword_map = KeyWord.getInstance()
				.getFirstwordMap();
		HashMap<String, Integer> firstword_fqc_map = KeyWord.getInstance()
				.getFirstwordFqcMap();
		HashMap<String, String> com_singleword_map = KeyWord.getInstance()
				.getCom1Map();
		HashMap<String, String> com_twomoreword_map = KeyWord.getInstance()
				.getCom2Map();

		int result_type = 0;
		int dgc_type = 0;
		char[] ch = content.toCharArray();
		StringBuffer ret = new StringBuffer();
		StringBuffer word = new StringBuffer();
		int start = 0;
		int step = 0;
		int max_length = 1;
		for (int i = 0; i < ch.length; i++) {
			if (step == 0) {
				start = i;
				step = 1;
			}
			char c = ch[i];
			word.append(c);
			String tmp = word.toString();
			if (i == start) {
				if (!firstword_fqc_map.containsKey(tmp.toLowerCase())) {
					step = 0;
					ret.append(ch[start]);
					word.delete(0, word.length());
					i = start;
					continue;
				} else {
					max_length = firstword_fqc_map.get(tmp.toLowerCase());
				}
			}
			if (fqc_map.containsKey(tmp.toLowerCase())) {
				ret.append(fqc_map.get(tmp.toLowerCase()));
				step = 0;
			} else if (word.length() >= max_length || i >= ch.length - 1) {
				step = 0;
				ret.append(ch[start]);
				word.delete(0, word.length());
				i = start;
			}
		}
		ch = ret.toString().toCharArray();
		word = new StringBuffer();
		start = 0;
		step = 0;
		if (sta.equals("0")) {
			for (int i = 0; i < ch.length; i++) {
				if (step == 0) {
					start = i;
					step = 1;
				}
				char c = ch[i];
				word.append(c);
				String tmp = word.toString();
				if (i == start) {
					if (com_singleword_map.containsKey(tmp.toLowerCase())) {
						result_type = 1;
						break;
					}
					if (!firstword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						word.delete(0, word.length());
						i = start;
						continue;
					} else {
						max_length = firstword_map.get(tmp.toLowerCase());
					}
				}
				if (jdc_map.containsKey(tmp.toLowerCase())) {
					result_type = 3;
					break;
				} else if (wfc_map.containsKey(tmp.toLowerCase())) {
					result_type = 2;
					break;
				} else if (com_twomoreword_map.containsKey(tmp.toLowerCase())) {
					result_type = 2;
					break;
				} else if (mgc_map.containsKey(tmp.toLowerCase())) {
					result_type = 1;
					break;
				} else if (word.length() > 1
						&& dgc_map.containsKey(tmp.toLowerCase())) {
					dgc_type = 4;
				} else if (word.length() >= max_length || i >= ch.length - 1) {
					step = 0;
					if (dgc_map.containsKey(String.valueOf(ch[start]))) {
						dgc_type = 4;
					}
					word.delete(0, word.length());
					i = start;
				}
			}
		} else if (sta.equals("1")) {
			for (int i = 0; i < ch.length; i++) {
				if (step == 0) {
					start = i;
					step = 1;
				}
				char c = ch[i];
				word.append(c);
				String tmp = word.toString();
				if (i == start) {
					if (!firstword_map.containsKey(tmp.toLowerCase())) {
						step = 0;
						word.delete(0, word.length());
						i = start;
						continue;
					} else {
						max_length = firstword_map.get(tmp.toLowerCase());
					}
				}
				if (jdc_map.containsKey(tmp.toLowerCase())) {
					result_type = 3;
					break;
				} else if (wfc_map.containsKey(tmp.toLowerCase())) {
					result_type = 2;
					break;
				} else if (mgc_map.containsKey(tmp.toLowerCase())) {
					result_type = 1;
					break;
				} else if (word.length() > 1
						&& dgc_map.containsKey(tmp.toLowerCase())) {
					dgc_type = 4;
				} else if (word.length() >= max_length || i >= ch.length - 1) {
					step = 0;
					//过滤单个字
					/*if (dgc_map.containsKey(String.valueOf(ch[start]))) {
						dgc_type = 4;
					}*/
					word.delete(0, word.length());
					i = start;
				}
			}
		}

		if (result_type == 0 && dgc_type == 4) {
			result_type = dgc_type;
		}

		return Integer.toString(result_type);

	}

}
